import Vue from "vue"
import Vuex from "vuex"
// 导入本地持久化token的方法
import * as auth from "../utils/auth"

// 导入登录接口请求方式
import { login } from "../api/user"

// 导入veux子模块
import chat from './modules/chat'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 存储登录用户token
    user: auth.getToken() || {},
  },

  mutations: {
    // 存
    setToken (state, token) {
      // 存到本地
      state.user = token
      // 存到硬盘
      auth.setToken(token)
      // console.log(token);
    },
    // 删
    delToken (state) {
      state.user = {}
      auth.delToken()
    },
  },
  actions: {
    // 登录接口请求
    async getToken ({ commit }, form) {
      const { data } = await login(form)
      // console.log('token:',data)
      commit("setToken", data)
      console.log(data)
    },
  },
  modules: {
    chat
  },
})
